<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/ajax.js"></script>
</head>
<body onload="getLoginedUser">
<header style="display: flex; justify-content: space-between;align-items: center">
    <h1>XXX系统</h1>
    <div id="myinfo">
        <a fref="#">登录</a>
        <a fref="#">注册</a>
    </div>
</header>
<main>
    <form action="login.s" method="post">
        账号:<input id="username"><br>
        密码:<input id="password" type="password"><br>
        验证码:
        <img src="vcode.s" height="22px" onclick="src='vcode.s?'+new Date()">
        <input id="vcode" placeholder="请求输入验证码"><br>
        <button type="button" onclick="login()">登录</button>
    </form>
</main>
<script>
    function login(){
        axios.get("login.do",{
            params:{
                username : username.value,
                password : password.value
            }
        },res=>{
            if (res.code == 1){
                if (res.data.requestURI){
                    location.href = res.data.requestURI
                }else {
                    myinfo.innerHTML = `欢迎 ${res.data} <a href ="logout.s">退出</a>`;
                }
            }else {
                alert(res.msg);
            }
        })
    }
    function getLoginedUser(){
        axios.get("myname.s",null,res=>{
            if (res.code == 1){
                myinfo.innerHTML = `欢迎 ${res.data} <a href ="logout.s">退出</a>`;
            }
        })
        if (location.search == '?code=1'){
            alert("请先登录系统")
        }
    }
</script>
</body>
</html>